package com.weitu.permission.resource.service.impl;

import com.weitu.base.entity.JsonTreeData;
import com.weitu.permission.resource.entity.Resource;
import com.weitu.permission.resource.mapper.ResourceMapper;
import com.weitu.permission.resource.service.ResourceService;
import com.weitu.util.TreeNodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by WT-SUN on 2017/3/14.
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String id) {
        return resourceMapper.deleteByPrimaryKey(id);
    }

    /**
     * 添加菜单
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(Resource record) {
        return resourceMapper.insertSelective(record);
    }

    /**
     * 查询菜单树 数据
     *
     * @param id
     * @return
     */
    @Override
    public List<Map> selectByPrimaryKey(String id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    /**
     * g更新菜单数据
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Resource record) {
        return resourceMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 查询菜单表格
     *
     * @param map
     * @return
     */
    @Override
    public List<Resource> queryTreeList(Map map) {
        return resourceMapper.queryTreeList(map);
    }

    /**
     * 统计菜单总条数
     *
     * @param map
     * @return
     */
    @Override
    public int queryTreeCount(Map map) {
        return resourceMapper.queryTreeCount(map);
    }

    /**
     * 查询一条菜单数据
     *
     * @param id
     * @return
     */
    @Override
    public Resource selectById(String id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public List<Map> selectResourcesByUserId(Map map) {
        return resourceMapper.selectResourcesByUserId(map);
    }

    @Override
    public List<Map> selectAllResourcesByUserId(String name, String deptId) {
        return resourceMapper.selectAllResourcesByUserId(name, deptId);
    }

    @Override
    public List<JsonTreeData> selectAllResourcesByAuthorityId(String authorityId) {
        List<Map> resources = resourceMapper.selectAllResourcesByAuthorityId(authorityId);
        List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
        for (Map<String, String> map : resources) {
            JsonTreeData treeData = new JsonTreeData();
            treeData.setId(map.get("id"));
            treeData.setPid(map.get("parentId"));
            treeData.setText(map.get("name"));
            if (map.get("checked") == null) {
                treeData.setChecked(false);
            } else {
                treeData.setChecked(true);
            }
            treeDataList.add(treeData);
        }
        List<JsonTreeData> newTreeDataList = TreeNodeUtil.getFatherNode(treeDataList);
        return newTreeDataList;
    }

    @Override
    public List<Map> selectAllButtonsByParentId(String parentId, String name, String deptId) {
        return resourceMapper.selectAllButtonsByParentId(parentId, name, deptId);
    }

    @Override
    public List<JsonTreeData> selectAllResourcsById(Map map) {
        List<Map> resources=resourceMapper.selectAllResourcsById(map);
        List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
        for (Map<String, String> map1 : resources) {
            JsonTreeData treeData = new JsonTreeData();
            treeData.setId(map1.get("id"));
            treeData.setPid(map1.get("parentId"));
            treeData.setText(map1.get("text"));
            treeData.setResUrl(map1.get("resUrl"));
            treeDataList.add(treeData);
        }
        List<JsonTreeData> newTreeDataList = TreeNodeUtil.getFatherNode(treeDataList);
        if (newTreeDataList.size()>0){
            newTreeDataList.get(0).setState("open");
        }
        return newTreeDataList;
    }

    @Override
    public Resource selectByCode(String code) {
        return resourceMapper.selectByCode(code);
    }

    @Override
    public List<Map> selectAllByUserId(Map map) {
        return resourceMapper.selectAllByUserId(map);
    }
}
